package org.point.common.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.point.common.util.StringOperateUtil;
import org.point.domain.MaxNo;
import org.point.service.ConvertRuleDetailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.support.ApplicationObjectSupport;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.alibaba.fastjson.JSONObject;

/**
 * 
 * @author Szqiu
 *
 */
public class BaseController extends ApplicationObjectSupport {

	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected ServletContext context;

	JSONObject jsonObj = new JSONObject();

	public StringOperateUtil strOperateUtil = new StringOperateUtil();

	/**
	 * 调用日志
	 */
	protected Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	@Qualifier("convertRuleDetailService")
	protected ConvertRuleDetailService convertRuleDetailService;

	/**
	 * 
	 * @Title: feedback @Description: @param @param response @param @param
	 *         content @param @throws IOException 设定文件 @return void 返回类型 @throws
	 */
	public void feedback(HttpServletResponse response, String content) throws IOException {
		response.setContentType("text/plain; charset=utf-8");
		PrintWriter writer = response.getWriter();
		writer.write(content);
		writer.flush();
		writer.close();
	}
	

	// 生成编号
	protected String getNextNo(String NoType) {
		String result = "";
		MaxNo maxNo = convertRuleDetailService.selectMaxNo();
		int temp = 0;
		if (NoType.equals("CT")) {
			// 用户编号(UA+6位数字)
			temp = maxNo.getCT_NO() + 1;
			result = NoType + getnum5(temp);
			maxNo.setCT_NO(temp);
		} else if (NoType.equals("CR")) {
			// 部门编号(DP+6位数字)
			temp = maxNo.getCR_NO() + 1;
			result = NoType + getnum5(temp);
			maxNo.setCR_NO(temp);
		} else if (NoType.equals("PRO"))

		{
			// 部门编号(DP+6位数字)
			temp = maxNo.getPRO_NO() + 1;
			result = NoType + getnum5(temp);
			maxNo.setPRO_NO(temp);
		} else if (NoType.equals("PT"))

		{
			// 部门编号(DP+6位数字)
			temp = maxNo.getPT_NO() + 1;
			result = NoType + getnum5(temp);
			maxNo.setPT_NO(temp);
		} else if (NoType.equals("PIO"))

		{
			// 部门编号(DP+6位数字)
			temp = maxNo.getPIO_NO() + 1;
			result = NoType + getnum5(temp);
			maxNo.setPIO_NO(temp);
		}else if(NoType.equals("CM")){
    		temp=maxNo.getCM_NO()+1;
    		result=NoType+getnum5(temp);
    		maxNo.setCM_NO(temp);
    	}else if(NoType.equals("CB")){
    		temp=maxNo.getCM_NO()+1;
    		result=NoType+getnum5(temp);
    		maxNo.setCM_NO(temp);
    	}else if(NoType.equals("CI")){
    		temp=maxNo.getCI_NO()+1;
    		result=NoType+getnum5(temp);
    		maxNo.setCI_NO(temp);
    	}

		convertRuleDetailService.updateMaxNo(maxNo);
		return result.toString();
	}

	protected String getnum5(int num) {
		String result = "";
		switch ((num + "").length()) {
		case 1:
			result = "0000" + num;
			break;
		case 2:
			result = "000" + num;
			break;
		case 3:
			result = "00" + num;
			break;
		case 4:
			result = "0" + num;
			break;
		default:
			result = num + "";
			break;
		}
		return result;
	}
	
	
	public static HttpServletRequest getRequest() {
		ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder
		.getRequestAttributes();
		return attrs.getRequest();
	} 
	
	public static HttpSession getSession() {
		HttpSession session = null;
		try {
		    session = getRequest().getSession();
		} catch (Exception e) {}
		    return session;
	} 

}